const { connection } = require('./index');

exports.getAccountInfoByUsername = (username) => {
  return new Promise(function (resolve, reject) {
    if (!username) {
      reject({ message: 'Username is not found in your request.' });
    }
    connection.query(
      `SELECT * FROM account WHERE username='${username}'`,
      function (err, result) {
        if (err) {
          reject({ message: 'Username is not found in your request.' });
        }
        resolve(result);
      }
    );
  });
};

exports.addAccountInfo = (addInfo) => {
  const { username, password } = addInfo;
  const sqlParams = [username, password];

  return new Promise(function (resolve, reject) {
    connection.query(
      'INSERT INTO account(username, password) VALUES(?,?)',
      sqlParams,
      function (err, result) {
        if (err) {
          reject({ message: 'Username is not found in your request.' });
        }
        resolve(result);
      }
    );
  });
};

exports.setPassword = (addInfo) => {
  const { username, password } = addInfo;
  const sqlParams = [username, password];

  return new Promise(function (resolve, reject) {
    connection.query(
      'UPDATE account SET password=? WHERE username=?',
      sqlParams,
      function (err, result) {
        if (err) {
          reject({ message: 'Username is not found in your request.' });
        }
        resolve(result);
      }
    );
  });
};

exports.deleteAccountInfoByUsername = (username) => {
  return new Promise(function (resolve, reject) {
    if (!username) {
      reject({ message: 'Username is not found in your request.' });
    }
    connection.query(
      `DELETE FROM account where username='${username}'`,
      function (err, result) {
        if (err) {
          reject({ message: 'Username is not found in your request.' });
        }
        resolve(result);
      }
    );
  });
};

exports.isAccountHasPortrait = (username) => {
  return new Promise(function (resolve, reject) {
    if (!username) {
      reject({ message: 'Username is not found in your request.' });
    }
    connection.query(
      `SELECT has_portrait FROM account WHERE username='${username}'`,
      function (err, result) {
        if (err) {
          reject({ message: 'Username is not found in your request.' });
        }
        resolve(result);
      }
    );
  });
};

exports.getAccountInfo = (username) => {
  return new Promise(function (resolve, reject) {
    connection.query(
      `SELECT age, description FROM account WHERE username='${username}'`,
      function (err, result) {
        if (err) {
          reject({ message: 'Username is not found in your request.' });
        }
        resolve(result);
      }
    );
  });
};

exports.setAccountInfo = (userInfo) => {
  return new Promise(function (resolve, reject) {
    connection.query(
      'UPDATE account SET age=?, description=? WHERE username=?',
      [userInfo.age, userInfo.description, userInfo.username],
      function (err, result) {
        if (err) {
          reject({ message: 'Username is not found in your request.' });
        }
        resolve(result);
      }
    );
  });
};

exports.setAccountHasPortrait = (username, filename) => {
  return new Promise(function (resolve, reject) {
    connection.query(
      'UPDATE account SET has_portrait=? WHERE username=?',
      [filename, username],
      function (err, result) {
        if (err) {
          reject({ message: 'Username is not found in your request.' });
        }
        resolve(result);
      }
    );
  });
};
